﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Common.Utils;
using Common.Utils.Helper;
using Object = UnityEngine.Object;
using Game;
using Game.Kernel;


namespace UIFramework
{
    public delegate void OnProgress(float progress);

    public class GameUIController : GameBaseController, IGameUIController
    {
        private Object m_prefab;
        private GameObject m_uiObj;
        private float m_progress;
        private event OnProgress OnProgress;

        public UIContext Context { get; private set; }
        public Transform CanvasTransform { get; private set; }

        // 构造函数
        public GameUIController(Transform canvasTransform, UIContext uiContext)
        {
            CanvasTransform = canvasTransform;
            Context = uiContext;
        }

        // 加载UI
        public void LoadUI()
        {
            ResManager.Instance.LoadAsync(Context.Path, OnLoadUICompleted, OnLoadUIProgress);
        }

        // UI控制器进入
        public virtual void OnEnter()
        {
            ((GameUIView)View).OnEnter(Context.Params);
        }

        // UI控制器退出
        public virtual void OnExit()
        {
            ((GameUIView)View).OnExit();
            UnlinkViewWithModel();
            Context.GameObject = null;

            View = null;
            Model = null;

            if (null != m_uiObj)
            {
                Destroy(m_uiObj);
            }
            m_uiObj = null;
        }

        // UI控制器暂停
        public virtual void OnPause()
        {
            ((GameUIView)View).OnPause();
        }

        // UI控制器恢复
        public virtual void OnResume()
        {
            ((GameUIView)View).OnResume();
        }

        public void OnLoadUIProgress(float _progress)
        {
            m_progress = _progress;
            if (null != OnProgress)
                OnProgress(m_progress);
        }

        public void OnLoadUICompleted(Object _prefabObj)
        {
            if (null == _prefabObj)
                throw new Exception("UIController Load UIObject Faield！ENUM_UITYPE:" + Context.Type);

            m_prefab = _prefabObj;
            m_uiObj = Instantiate(_prefabObj) as GameObject;

            if (null == m_uiObj)
                throw new Exception("UIController Instantiate UIObject Faield！ENUM_UITYPE:" + Context.Type);

            UnityHelper.AddChildToParent(CanvasTransform, m_uiObj.transform);

            View = GetUIView(m_uiObj);
            Model = GetUIModel();

            Context.GameObject = m_uiObj;
            LinkViewToModel();

            OnEnter();
        }

        public void AddEventHandler(OnProgress handler)
        {
            OnProgress += handler;
        }

        public void RemoveEventHandler(OnProgress handler)
        {
            OnProgress -= handler;
        }

        public GameUIView GetUIView(GameObject go)
        {
            string uiConfigModule = GameUIManager.Instance.GetUIConfigModule(Context.Type);
            string uiConfigView = GameUIManager.Instance.GetUIConfigView(Context.Type);
            if (null == uiConfigView || null == uiConfigModule)
                throw new Exception("UIController Get View Class Name Faield！ENUM_UITYPE:" + Context.Type);

            string viewClassName = string.Concat(GameDefine.GAME_NAMESPACE_LOGIC, ".", uiConfigModule, ".", uiConfigView);
            Type scriptType = Type.GetType(viewClassName);
            GameUIView guv = (GameUIView)go.AddComponent(scriptType);       // mark: 添加脚本到view
            return guv;
        }

        public GameUIModel GetUIModel()
        {
            string uiConfigModule = GameUIManager.Instance.GetUIConfigModule(Context.Type);
            string uiConfigModel = GameUIManager.Instance.GetUIConfigModel(Context.Type);
            if (null == uiConfigModule || null == uiConfigModel)
                throw new Exception("UIController Get Model Class Name Faield！ENUM_UITYPE:" + Context.Type);
            return ReflectionHelper.CreateInstance<GameUIModel>(string.Concat(GameDefine.GAME_NAMESPACE_LOGIC, ".", uiConfigModule), uiConfigModel);
        }
    }
}
